/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OfficeOpenXMLInterop;

namespace Theme_Builder
{
    public partial class EditScene : UserControl
    {
        public EditScene()
        {
            InitializeComponent();
        }

        public OfficeOpenXMLInterop.CT_Scene3D Scene3D
        {
            get
            {
                CT_Scene3D scene = new CT_Scene3D();

                scene.camera = new CT_Camera();

                // Set the camera if there is one defined
                if (this.comboBoxRotation.SelectedItem != null)
                {
                    scene.camera.prst = this.ParseCamera(this.comboBoxRotation.SelectedItem.ToString());

                    // Handle any custom rotations the user may have input
                    if (scene.camera.prst == ST_PresetCameraType.orthographicFront)
                    {
                        scene.camera.rot = new CT_SphereCoords();
                        scene.camera.rot.lat = (int)Math.Round(float.Parse(this.textBoxLat.Text) * 60000f);
                        scene.camera.rot.lon = (int)Math.Round(float.Parse(this.textBoxLon.Text) * 60000f);
                        scene.camera.rot.rev = (int)Math.Round(float.Parse(this.textBoxRev.Text) * 60000f);
                    }
                }

                // Handle the perspective
                int foo = (int)Math.Round(float.Parse(this.textBoxFoV.Text) * 60000f);
                if (foo != 0)
                {
                    scene.camera.fov = foo;
                    scene.camera.fovSpecified = true;
                }

                scene.lightRig = new CT_LightRig();

                // Set the light rig if there is one defined
                if (this.comboBoxLightRig.SelectedItem != null)
                {
                    scene.lightRig.rig = this.ParseLightType(this.comboBoxLightRig.SelectedItem.ToString());
                }

                // Set the direction of the light rig
                if (this.comboBoxDirection.SelectedItem != null)
                {
                    scene.lightRig.dir = this.ParseLightDirection(this.comboBoxDirection.SelectedItem.ToString());
                }

                // Handle the rotation angle if there is one defined
                foo = (int)Math.Round(float.Parse(this.textBoxAngle.Text) * 60000f);
                if (foo != 0)
                {
                    scene.lightRig.rot = new CT_SphereCoords();
                    scene.lightRig.rot.rev = foo;
                }

                return scene;
            }
            set
            {
                // Handle the camera
                if (value.camera != null)
                {
                    this.comboBoxRotation.SelectedIndex = this.ParseCamera(value.camera.prst);

                    // Handle any custom values that may be present
                    if (value.camera.prst == ST_PresetCameraType.orthographicFront)
                    {
                        if (value.camera.rot != null)
                        {
                            this.textBoxLat.Text = ((float)value.camera.rot.lat / 60000f).ToString();
                            this.textBoxLon.Text = ((float)value.camera.rot.lon / 60000f).ToString();
                            this.textBoxRev.Text = ((float)value.camera.rot.rev / 60000f).ToString();
                        }
                    }

                    // Handle the perspective
                    if (value.camera.fovSpecified)
                    {
                        this.textBoxFoV.Text = ((float)value.camera.fov / 60000f).ToString();
                    }
                }

                // Handle the light rig
                if (value.lightRig != null)
                {
                    this.comboBoxLightRig.SelectedIndex = this.ParseLightType(value.lightRig.rig);
                    this.comboBoxDirection.SelectedIndex = this.ParseLightDirection(value.lightRig.dir);

                    // Handle the angle if there is one defined
                    if (value.lightRig.rot != null)
                    {
                        this.textBoxAngle.Text = ((float)value.lightRig.rot.rev / 60000f).ToString();
                    }
                }
            }
        }

        private void comboBoxRotation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxRotation.SelectedItem != null)
            {
                if (this.comboBoxRotation.SelectedItem.ToString() == "Ortho Front (None)")
                {
                    this.textBoxLat.Enabled = true;
                    this.textBoxLon.Enabled = true;
                    this.textBoxRev.Enabled = true;
                }
                else
                {
                    this.textBoxLat.Enabled = false;
                    this.textBoxLon.Enabled = false;
                    this.textBoxRev.Enabled = false;
                }
            }
        }

        private OfficeOpenXMLInterop.ST_PresetCameraType ParseCamera(string camera)
        {
            switch (camera)
            {
                case "Ortho Front (None)":
                    return ST_PresetCameraType.orthographicFront;
                case "Iso Top Up":
                    return ST_PresetCameraType.isometricTopUp;
                case "Iso Top Down":
                    return ST_PresetCameraType.isometricTopDown;
                case "Iso Bottom Up":
                    return ST_PresetCameraType.isometricBottomUp;
                case "Iso Bottom Down":
                    return ST_PresetCameraType.isometricBottomDown;
                case "Iso Left Up":
                    return ST_PresetCameraType.isometricLeftUp;
                case "Iso Left Down":
                    return ST_PresetCameraType.isometricLeftDown;
                case "Iso Right Up":
                    return ST_PresetCameraType.isometricRightUp;
                case "Iso Right Down":
                    return ST_PresetCameraType.isometricRightDown;
                case "Iso Off Axis 1 Left":
                    return ST_PresetCameraType.isometricOffAxis1Left;
                case "Iso Off Axis 1 Right":
                    return ST_PresetCameraType.isometricOffAxis1Right;
                case "Iso Off Axis 1 Top":
                    return ST_PresetCameraType.isometricOffAxis1Top;
                case "Iso Off Axis 2 Left":
                    return ST_PresetCameraType.isometricOffAxis2Left;
                case "Iso Off Axis 2 Right":
                    return ST_PresetCameraType.isometricOffAxis2Right;
                case "Iso Off Axis 2 Top":
                    return ST_PresetCameraType.isometricOffAxis2Top;
                case "Iso Off Axis 3 Left":
                    return ST_PresetCameraType.isometricOffAxis3Left;
                case "Iso Off Axis 3 Right":
                    return ST_PresetCameraType.isometricOffAxis3Right;
                case "Iso Off Axis 3 Bottom":
                    return ST_PresetCameraType.isometricOffAxis3Bottom;
                case "Iso Off Axis 4 Left":
                    return ST_PresetCameraType.isometricOffAxis4Left;
                case "Iso Off Axis 4 Right":
                    return ST_PresetCameraType.isometricOffAxis4Right;
                case "Iso Off Axis 4 Bottom":
                    return ST_PresetCameraType.isometricOffAxis4Bottom;
                case "Oblique Top Left":
                    return ST_PresetCameraType.obliqueTopLeft;
                case "Oblique Top":
                    return ST_PresetCameraType.obliqueTop;
                case "Oblique Top Right":
                    return ST_PresetCameraType.obliqueTopRight;
                case "Oblique Left":
                    return ST_PresetCameraType.obliqueLeft;
                case "Oblique Right":
                    return ST_PresetCameraType.obliqueRight;
                case "Oblique Bottom left":
                    return ST_PresetCameraType.obliqueBottomLeft;
                case "Oblique Bottom":
                    return ST_PresetCameraType.obliqueBottom;
                case "Oblique Bottom Right":
                    return ST_PresetCameraType.obliqueBottomRight;
                case "Perspective Front":
                    return ST_PresetCameraType.perspectiveFront;
                case "Perspective Left":
                    return ST_PresetCameraType.perspectiveLeft;
                case "Perspective Right":
                    return ST_PresetCameraType.perspectiveRight;
                case "Perspective Above":
                    return ST_PresetCameraType.perspectiveAbove;
                case "Perspective Below":
                    return ST_PresetCameraType.perspectiveBelow;
                case "Above Left":
                    return ST_PresetCameraType.perspectiveAboveLeftFacing;
                case "Above Right":
                    return ST_PresetCameraType.perspectiveAboveRightFacing;
                case "Contrasting Left":
                    return ST_PresetCameraType.perspectiveContrastingLeftFacing;
                case "Contrasting Right":
                    return ST_PresetCameraType.perspectiveContrastingRightFacing;
                case "Heroic Left":
                    return ST_PresetCameraType.perspectiveHeroicLeftFacing;
                case "Heroic Right":
                    return ST_PresetCameraType.perspectiveHeroicRightFacing;
                case "Heroic Extreme Left":
                    return ST_PresetCameraType.perspectiveHeroicExtremeLeftFacing;
                case "Heroic Extreme Right":
                    return ST_PresetCameraType.perspectiveHeroicExtremeRightFacing;
                case "Relaxed":
                    return ST_PresetCameraType.perspectiveRelaxed;
                case "Relaxed Moderately":
                    return ST_PresetCameraType.perspectiveRelaxedModerately;
                default:
                    return ST_PresetCameraType.orthographicFront;
            }
        }

        private int ParseCamera(OfficeOpenXMLInterop.ST_PresetCameraType camera)
        {
            switch (camera)
            {
                case ST_PresetCameraType.orthographicFront:
                    return 0;
                case ST_PresetCameraType.isometricTopUp:
                    return 1;
                case ST_PresetCameraType.isometricTopDown:
                    return 2;
                case ST_PresetCameraType.isometricBottomUp:
                    return 3;
                case ST_PresetCameraType.isometricBottomDown:
                    return 4;
                case ST_PresetCameraType.isometricLeftUp:
                    return 5;
                case ST_PresetCameraType.isometricLeftDown:
                    return 6;
                case ST_PresetCameraType.isometricRightUp:
                    return 7;
                case ST_PresetCameraType.isometricRightDown:
                    return 8;
                case ST_PresetCameraType.isometricOffAxis1Left:
                    return 9;
                case ST_PresetCameraType.isometricOffAxis1Right:
                    return 10;
                case ST_PresetCameraType.isometricOffAxis1Top:
                    return 11;
                case ST_PresetCameraType.isometricOffAxis2Left:
                    return 12;
                case ST_PresetCameraType.isometricOffAxis2Right:
                    return 13;
                case ST_PresetCameraType.isometricOffAxis2Top:
                    return 14;
                case ST_PresetCameraType.isometricOffAxis3Left:
                    return 15;
                case ST_PresetCameraType.isometricOffAxis3Right:
                    return 16;
                case ST_PresetCameraType.isometricOffAxis3Bottom:
                    return 17;
                case ST_PresetCameraType.isometricOffAxis4Left:
                    return 18;
                case ST_PresetCameraType.isometricOffAxis4Right:
                    return 19;
                case ST_PresetCameraType.isometricOffAxis4Bottom:
                    return 20;
                case ST_PresetCameraType.obliqueTopLeft:
                    return 21;
                case ST_PresetCameraType.obliqueTop:
                    return 22;
                case ST_PresetCameraType.obliqueTopRight:
                    return 23;
                case ST_PresetCameraType.obliqueLeft:
                    return 24;
                case ST_PresetCameraType.obliqueRight:
                    return 25;
                case ST_PresetCameraType.obliqueBottomLeft:
                    return 26;
                case ST_PresetCameraType.obliqueBottom:
                    return 27;
                case ST_PresetCameraType.obliqueBottomRight:
                    return 28;
                case ST_PresetCameraType.perspectiveFront:
                    return 29;
                case ST_PresetCameraType.perspectiveLeft:
                    return 30;
                case ST_PresetCameraType.perspectiveRight:
                    return 31;
                case ST_PresetCameraType.perspectiveAbove:
                    return 32;
                case ST_PresetCameraType.perspectiveBelow:
                    return 33;
                case ST_PresetCameraType.perspectiveAboveLeftFacing:
                    return 34;
                case ST_PresetCameraType.perspectiveAboveRightFacing:
                    return 35;
                case ST_PresetCameraType.perspectiveContrastingLeftFacing:
                    return 36;
                case ST_PresetCameraType.perspectiveContrastingRightFacing:
                    return 37;
                case ST_PresetCameraType.perspectiveHeroicLeftFacing:
                    return 38;
                case ST_PresetCameraType.perspectiveHeroicRightFacing:
                    return 39;
                case ST_PresetCameraType.perspectiveHeroicExtremeLeftFacing:
                    return 40;
                case ST_PresetCameraType.perspectiveHeroicExtremeRightFacing:
                    return 41;
                case ST_PresetCameraType.perspectiveRelaxed:
                    return 42;
                case ST_PresetCameraType.perspectiveRelaxedModerately:
                    return 43;
                default:
                    return 0;
            }
        }

        private OfficeOpenXMLInterop.ST_LightRigType ParseLightType(string light)
        {
            switch (light)
            {
                case "Three Point":
                    return ST_LightRigType.threePt;
                case "Balanced":
                    return ST_LightRigType.balanced;
                case "Soft":
                    return ST_LightRigType.soft;
                case "Harsh":
                    return ST_LightRigType.harsh;
                case "Flood":
                    return ST_LightRigType.flat;
                case "Contrasting":
                    return ST_LightRigType.contrasting;
                case "Morning":
                    return ST_LightRigType.morning;
                case "Sunrise":
                    return ST_LightRigType.sunrise;
                case "Sunset":
                    return ST_LightRigType.sunset;
                case "Chilly":
                    return ST_LightRigType.chilly;
                case "Freezing":
                    return ST_LightRigType.freezing;
                case "Flat":
                    return ST_LightRigType.flat;
                case "Two Point":
                    return ST_LightRigType.twoPt;
                case "Glow":
                    return ST_LightRigType.glow;
                case "Bright Room":
                    return ST_LightRigType.brightRoom;
                default:
                    return ST_LightRigType.threePt;
            }
        }

        private int ParseLightType(OfficeOpenXMLInterop.ST_LightRigType light)
        {
            switch (light)
            {
                case ST_LightRigType.threePt:
                    return 0;
                case ST_LightRigType.balanced:
                    return 1;
                case ST_LightRigType.soft:
                    return 2;
                case ST_LightRigType.harsh:
                    return 3;
                case ST_LightRigType.flood:
                    return 4;
                case ST_LightRigType.contrasting:
                    return 5;
                case ST_LightRigType.morning:
                    return 6;
                case ST_LightRigType.sunrise:
                    return 7;
                case ST_LightRigType.sunset:
                    return 8;
                case ST_LightRigType.chilly:
                    return 9;
                case ST_LightRigType.freezing:
                    return 10;
                case ST_LightRigType.flat:
                    return 11;
                case ST_LightRigType.twoPt:
                    return 12;
                case ST_LightRigType.glow:
                    return 13;
                case ST_LightRigType.brightRoom:
                    return 14;
                default:
                    return 0;
            }
        }

        private OfficeOpenXMLInterop.ST_LightRigDirection ParseLightDirection(string light)
        {
            switch (light)
            {
                case "Top Left":
                    return ST_LightRigDirection.tl;
                case "Top":
                    return ST_LightRigDirection.t;
                case "Top Right":
                    return ST_LightRigDirection.tr;
                case "Left":
                    return ST_LightRigDirection.l;
                case "Right":
                    return ST_LightRigDirection.r;
                case "Bottom Left":
                    return ST_LightRigDirection.bl;
                case "Bottom":
                    return ST_LightRigDirection.b;
                case "Bottom Right":
                    return ST_LightRigDirection.br;
                default:
                    return ST_LightRigDirection.t;
            }
        }

        private int ParseLightDirection(OfficeOpenXMLInterop.ST_LightRigDirection light)
        {
            switch (light)
            {
                case ST_LightRigDirection.tl:
                    return 0;
                case ST_LightRigDirection.t:
                    return 1;
                case ST_LightRigDirection.tr:
                    return 2;
                case ST_LightRigDirection.l:
                    return 3;
                case ST_LightRigDirection.r:
                    return 4;
                case ST_LightRigDirection.bl:
                    return 5;
                case ST_LightRigDirection.b:
                    return 6;
                case ST_LightRigDirection.br:
                    return 7;
                default:
                    return 1;
            }
        }
    }
}
